<?php
namespace app\admin\controller;
use think\Request;

class Permission  extends Common
{
    public function admin_list()
    {
        $search=input('get.search','','htmlspecialchars');
        $page_search=$search;
        $pages=5;
        $db=db('admin');
        if($search){
        
            $db->where('rolename|phone','like','%'.$search.'%');
    
        }
        $admin_list=$db->alias('a')->field('a.*,b.rolename as rolename')->join('role b','a.role_id=b.id')->order('id')->paginate($pages,false,['query'=>['search'=>$page_search]]);

        $current_page=input('get.page','1','intval');

        $page=$admin_list->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $request=Request::instance();
        if($request->isAjax()){
            return json(['admin_list'=>$admin_list,'page'=>$page,'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('admin_list' , $admin_list);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function add_admin()
    {
        $role=db('role')->select();
        $this->assign('role',$role);
        return view();
    }
    public function admin_add(){
        $post['user']=input('post.user','','htmlspecialchars');
        $post['age']=input('post.age','','htmlspecialchars');
        $post['nickname']=input('post.nickname','','htmlspecialchars');
        $post['email']=input('post.email','','htmlspecialchars');
        $post['phone']=input('post.phone','','htmlspecialchars');
        $post['pwd']=md5(input('post.pwd','','trim'));
        $post['inputtime']=date('Y-m-d h:m:s');
        $post['role_id']=input('post.role');
        // dump($post);die;
        $role=validate('role');           
        if(!$role->check($post)){
            $this->error($role->getError());
        }
        $id=db('admin')->insertGetId($post);
        $date['admin_id']=$id;
        $date['role_id']=input('post.role');        
        db('admin_role')->insert($date);
        if($id){
            $this->success('添加成功',url('admin_list'));
        }else{
            $this->error('添加失败');
        }
    }
    public function admin_delete(){
        $id=input('get.id');
        $id=db('admin')->delete($id);
    }
    public function code_user(){
        $user=input('get.user');
        $id=db('admin')->where('user',$user)->find();
        if($id){
            return true;
        }
    }
    public function edit_admin()
    {
        $id=input('get.id');
        $role=db('role')->select();
        $res=db('admin')->alias('a')->field('a.*,b.rolename as rolename')->join('role b','a.role_id=b.id')->where('a.id',$id)->find();
        $this->assign('res',$res);
        $this->assign('role',$role);   
        return view();
    }
    public function admin_edit(){
        $post=input('post.');
        $result=db('admin')->where('id',$post['id'])->update($post);
        if($result){
            $this->success('更新成功' , url('admin_list') );
        }
    }
    public function role_list()
    {
        $search=input('get.search','','htmlspecialchars');
        $page_search=$search;
        $pages=5;
        $db=db('role');
        if($search){
        
            $db->where('rolename','like','%'.$search.'%');
    
        }
        $role_list=$db->paginate($pages,false,['query'=>['search'=>$page_search]]);

        $current_page=input('get.page','1','intval');

        $page=$role_list->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $request=Request::instance();
        if($request->isAjax()){
            return json(['role_list'=>$role_list,'page'=>$page,'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('role_list' , $role_list);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function edit_role() {
        $roleId = input('get.id');
        $role = db('role')->find($roleId);
        $permission = db('permission')->order('desc')->select();
        $permission = rebuildArr1($permission);
        $permissions_id = db('RolePermission')->where('role_id' , $roleId)->select();
        $having_permissions_id = [];
        foreach ($permissions_id as $key => $value)
        {
            $having_permissions_id[] = $value['permission_id'];
        }
        $this->assign('having_permissions_id',$having_permissions_id);
        $this->assign('permission' , $permission);
        $this->assign('role' , $role);
        return view();
    }
    public function role_store ()
    {
        $post = input('post.');
        db('RolePermission')->where('role_id' , $post['role_id'])->delete();
        foreach ($post['permission_id'] as $key => $value) {
            $insertData = ['role_id' => $post['role_id'] , 'permission_id' => $value];
            db('RolePermission')->insert($insertData);
        }
        $this->success('修改成功', url('role_list'));
    }
    public function role_delete(){
        $id=input('get.id');
        $id=db('role')->delete($id);
    } 
    public function permission_list()
    {
        $search=input('get.search','','htmlspecialchars');
        $page_search=$search;
        $pages=5;
        $db=db('permission');
        if($search){
        
            $db->where('name|personal_name','like','%'.$search.'%');
    
        }
        $permission_list=$db->paginate($pages,false,['query'=>['search'=>$page_search]]);

        $current_page=input('get.page','1','intval');

        $page=$permission_list->render();
        $page=str_replace(
            ['class="pagination"','class="disabled"','class="active"'],
            ['class="am-pagination tpl-pagination"','class="am-disabled"','class="am-active"'],
            $page
        );
        $request=Request::instance();
        if($request->isAjax()){
            return json(['permission_list'=>$permission_list,'page'=>$page,'current_page'=>$current_page]);
        }
        $this->assign('page' , $page);
        $this->assign('permission_list' , $permission_list);
        $this->assign('current_page' , $current_page);
        return view();
    }
    public function add_permission(){
        $permission = db('permission')->where('pid',0)->select();
        $this->assign('permission',$permission);
        return view();
    }
    public function store ()
    {
        $post=input('post.');
        $post['type']=input('post.type','1');
        db('permission')->insert($post);
        $this->success('添加权限成功', url('permission_list'));
    }
    public function permission_delete(){
        $id=input('get.id');
        $id=db('permission')->delete($id);
    }
}
